<template>
  <el-dialog
    :visible.sync="dialogVisible"
    title="表格操作"
    :close-on-click-modal="false"
    @close="dialogVisible = false"
    append-to-body
  >
    <el-form :model="screenItem">
      <el-form-item label="筛选名" :label-width="'100px'">
        <el-input v-model="screenItem.label" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="默认文案" :label-width="'100px'">
        <el-input
          v-model="screenItem.placeholder"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="关联数据" :label-width="'100px'">
        <el-select
          v-if="request"
          v-model="screenItem.selectModel"
          filterable
          placeholder="关联数据"
        >
          <template v-for="(item, idx) in JSON.parse(request.params)">
            <el-option
              :key="idx"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="筛选方式" :label-width="'100px'">
        <el-select
          v-if="request"
          v-model="screenItem.selectType"
          filterable
          placeholder="筛选方式"
        >
          <el-option label="手动输入" value="input"></el-option>
          <el-option label="下拉选择" value="select"></el-option>
          <el-option label="下拉选择(api数据)" value="apiselect"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="“是否多选”"
        label-width="100px"
        v-if="
          screenItem.selectType === 'select' ||
            screenItem.selectType === 'apiselect'
        "
      >
        <el-select v-model="screenItem.multiSelect" placeholder="否">
          <el-option label="是" :value="true"></el-option>
          <el-option label="否" :value="false"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="screenItem.selectType === 'select'"
        label="可选项"
        :label-width="'100px'"
      >
        <template v-for="(item, index) in screenItem.options">
          <div :key="index">
            <el-input
              v-model="screenItem.options[index].name"
              size="small"
              placeholder="请输入选项名"
              style="width: 180px"
            >
            </el-input>
            <el-input
              v-model="screenItem.options[index].value"
              size="small"
              placeholder="请输入选项值"
              style="width: 200px"
            >
            </el-input>
          </div>
        </template>
        <el-button @click="screenItem.options.push({ name: '', value: null })"
          >新增一项</el-button
        >
      </el-form-item>
      <template v-if="screenItem.selectType === 'apiselect'">
        <el-form-item label="api" label-width="100px">
          <el-input
            v-model="screenItem.api"
            placeholder="输入获取所有选项的api"
          ></el-input>
        </el-form-item>
        <el-form-item label="请求参数" label-width="100px">
          <el-input
            v-model="screenItem.requestParams"
            placeholder="输入获取所有选项的api请求参数"
          ></el-input>
        </el-form-item>
        <el-form-item label="返回结果列表字段" label-width="100px">
          <el-input
            v-model="screenItem.listKey"
            placeholder="返回结果中列表项对应的字段"
          ></el-input>
        </el-form-item>
        <el-form-item label="显示字段" label-width="100px">
          <el-input
            v-model="screenItem.labelKey"
            placeholder="选项显示的值对应字段"
          ></el-input>
        </el-form-item>
        <el-form-item label="值字段" label-width="100px">
          <el-input
            v-model="screenItem.valueKey"
            placeholder="选项的值的对应字段"
          ></el-input>
        </el-form-item>
      </template>
    </el-form>
    <span slot="footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="onOK">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { deepClone } from '@/utils'

export default {
  name: 'FilterEditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    value: {
      type: Object
    },
    request: {
      type: Object
    }
  },
  data() {
    return {
      editDialogVis: false,
      operationToEdit: {},
      screenItem: this.value,
      tableColumnTypes: [
        '字符串',
        '日期',
        '时间',
        '整数',
        '2位小数',
        '4位小数',
        '枚举',
        '时分',
        '时分秒',
        '开启服务类型',
        '国家',
        '聊天内容解析',
        '富文本'
      ]
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible')
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.screenItem = deepClone(this.value)
        console.log(this.screenItem)
      }
    }
  },
  methods: {
    onOK() {
      this.$emit('success', this.screenItem)
    }
  }
}
</script>

<style scoped></style>
